package org.dromara.system.controller.system;

import java.util.List;

import cn.hutool.core.lang.tree.Tree;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.components.sysRegion.domain.vo.SysRegionVo;
import org.dromara.components.sysRegion.domain.bo.SysRegionBo;
import org.dromara.components.sysRegion.service.ISysRegionService;

/**
 * 省市区街道信息
 *
 * @author wdm
 * @date 2024-02-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/sysRegion")
public class SysRegionController extends BaseController {

    private final ISysRegionService sysRegionService;

    /**
     * 查询省市区街道信息列表
     */
    @SaCheckPermission("system:sysRegion:list")
    @GetMapping("/list")
    public R<List<SysRegionVo>> list(SysRegionBo bo) {
        SysRegionBo nullBo = new SysRegionBo();
        //默认查询一级结点
        if(nullBo.toString().equals(bo.toString())){
            bo.setParentId(0L);
        }
        List<SysRegionVo> list = sysRegionService.queryList(bo);
        return R.ok(list);
    }
    /**
     * 查询省市区街道信息列表
     */
    @SaCheckPermission("system:sysRegion:treeList")
    @GetMapping("/treeList")
    public R<List<Tree<Long>>> treeList(SysRegionBo bo) {
        List<SysRegionVo> list = sysRegionService.queryList(bo);
        List<Tree<Long>> treeList = sysRegionService.buildMenuTreeSelect(list);
        return R.ok(treeList);
    }

    /**
     * 导出省市区街道信息列表
     */
    @SaCheckPermission("system:sysRegion:export")
    @Log(title = "省市区街道信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysRegionBo bo, HttpServletResponse response) {
        List<SysRegionVo> list = sysRegionService.queryList(bo);
        ExcelUtil.exportExcel(list, "省市区街道信息", SysRegionVo.class, response);
    }

    /**
     * 获取省市区街道信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:sysRegion:query")
    @GetMapping("/{id}")
    public R<SysRegionVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(sysRegionService.queryById(id));
    }

    /**
     * 新增省市区街道信息
     */
    @SaCheckPermission("system:sysRegion:add")
    @Log(title = "省市区街道信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysRegionBo bo) {
        return toAjax(sysRegionService.insertByBo(bo));
    }

    /**
     * 修改省市区街道信息
     */
    @SaCheckPermission("system:sysRegion:edit")
    @Log(title = "省市区街道信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysRegionBo bo) {
        return toAjax(sysRegionService.updateByBo(bo));
    }

    /**
     * 删除省市区街道信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:sysRegion:remove")
    @Log(title = "省市区街道信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(sysRegionService.deleteByType(List.of(ids), true));
    }
}
